﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OverStore.Runtime.Storage;
using OverStore.Exceptions.Configuration;
using OverStore.Configuration.Validation;
using OverStore.Runtime.OverStoreContext;
using OverStore.Runtime.ObjectRepository.IdentifierProvider;
using OverStore.Runtime.ObjectRepository.Reader;

namespace OverStore.Configuration.ObjectRepository.Reader
{
    /// <summary>
    /// Configuration parameters for parameterless reader.
    /// </summary>
    /// <typeparam name="TInstance">The type of the instance.</typeparam>
    /// <typeparam name="TIdentifier">The type of the identifier.</typeparam>
    public class ParameterlessReaderConfiguration<TInstance, TIdentifier> : IComponentConfiguration<IReader<TInstance, TIdentifier>>
        where TInstance : class
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ParameterlessReaderConfiguration&lt;TInstance, TIdentifier&gt;"/> class.
        /// </summary>
        public ParameterlessReaderConfiguration()
        {
            UseResultSetForObjectConstruction = true;
        }

        /// <summary>
        /// <para>Get or set true if this reader will use data that read
        /// from data source for fill objec data; otherwise, reader should use object identifier for access instance 
        /// using <see cref="OverStore.Runtime.ObjectRepository.IEntityRegistry{TItem, TIdentifier}"/>.</para>
        /// </summary>
        public bool UseResultSetForObjectConstruction { get; set; }

        /// <summary>
        /// Get or set read command uses for read data from storage.
        /// </summary>
        public IComponentConfiguration<ICommand> ReadCommand { get; set; }

        /// <summary>
        /// Gets or sets the identifier provider uses for obtain identifiers from data.
        /// </summary>
        public IComponentConfiguration<IIdentifierProvider<TIdentifier>> IdentifierProvider { get; set; }

        /// <summary>
        /// Creates the new instance of <typeparamref name="TComponent"/> type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of <typeparamref name="TComponent"/> class.
        /// </returns>
        public IReader<TInstance, TIdentifier> CreateComponent(IOverStoreContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (ReadCommand == null)
                throw this.ConfigurationException("ReadCommand is missing.");
            if (IdentifierProvider == null)
                throw this.ConfigurationException("Identifier Provider is missing.");

            return new ParameterlessReader<TInstance, TIdentifier>(
                context.PersistenceServiceProvider,
                context.DataStorage,
                ReadCommand.CreateComponent(context),
                IdentifierProvider.CreateComponent(context),
                context.Logger,
                UseResultSetForObjectConstruction);
        }

        /// <summary>
        /// Perform component validation. Method should validate only
        /// component configuration itself, not sub components.
        /// If configuration is valid, null result should returns.
        /// </summary>
        /// <returns>
        ///     <see cref="T:OverStore.Configuration.ValidationResults"/> describes errors found in component configuration.
        /// </returns>
        public ValidationResult Validate()
        {
            return new ValidationResult(this)
                .AddError(ReadCommand == null, "Read Command is missing")
                .AddError(IdentifierProvider == null, "Identifier Provider is missing.")
                .ValidateToNestedResult(ReadCommand);
        }
    }
}
